
const {ccclass, property} = cc._decorator;

@ccclass
export default class PokerModelTs extends cc.Component {

    @property(cc.Node)
    iconMix : cc.Node = null;

    _isSelected = false;
    pokerObj = null;
    _hasFromData = false;

    dragFromPos = null;
    dragFromScale = null;
    dragOriZIndex = null;
    handCardOriY = null;
    deckOriY = null;
    areaDis = null;

    //根节点是弃牌堆
    fromDisPokerStack = null;
    //在弃牌堆中，能否被摸(只有弃牌堆的第一张可以被摸)
    _disPokerDragable = null;
    //根节点是手牌节点
    fromHand : boolean = false;
    //是否稳定地放在手牌里，不拖拽，无动画
    inHandStatic : boolean = true;
    //是否处于开局动画阶段
    inDispatchAnim : boolean = false;
    //是否正在播放弃牌动画
    inDiscardAnim : boolean = false;
    //玩家已经拿起这张牌但还没有放到手牌有效区域
    grabing : boolean = false;

    startPos = null;
    dragLasting = null;
    draggingNode = null;

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._initPoker()
    }

    start () {

    }

    update(dt){
        //control the poker size by distance with deck and hand pokers
        if(this.inDispatchAnim) return
        if(this.dragLasting){
            cc.log("拖拽中")
            let worldY = this.node.parent.convertToWorldSpaceAR(this.node.position).y
            if(this.fromDisPokerStack){
                if(worldY > this.deckOriY) return
                if(worldY < this.handCardOriY) return
                let dis = this.deckOriY - worldY
                let disPer = dis/this.areaDis
                this.setPokerScale(0.8 + 0.2*disPer)
            }else {
                if(worldY > this.deckOriY) return
                if(worldY < this.handCardOriY) return
                let dis = worldY-this.handCardOriY
                let disPer = dis/this.areaDis
                this.setPokerScale(1 - 0.2*disPer)
            }
        }else {
            //拖拽的时候，一个0。8一个1是因为父节点是手牌
            //不拖拽时，scale统一是1就可以，因为父节点是弃牌堆的话，弃牌堆自己有0.8的缩放
            let worldY = this.node.parent.convertToWorldSpaceAR(this.node.position).y
            if(this.fromDisPokerStack){ //弃牌时，牌会销毁，并重新在弃牌堆生成一个，所以这里只包括飞向弃牌堆，最终状态是0.8就对了
                if(worldY > this.deckOriY) return
                if(worldY < this.handCardOriY) return
                let dis = this.deckOriY - worldY
                let disPer = dis/this.areaDis
                this.setPokerScale(0.8 + 0.2*disPer)
            }else if(this.inHandStatic){    //在手牌中静静呆着
                this.setPokerScale(1)
            }else {     //飞向手牌
                if(worldY > this.deckOriY) return
                if(worldY < this.handCardOriY) return
                let dis = Math.abs(this.handCardOriY - worldY)
                let disPer = dis/this.areaDis
                this.setPokerScale(1 - 0.2*disPer)
            }
            // let worldY = this.node.parent.convertToWorldSpaceAR(this.node.position).y
            // if(this.fromDisPokerStack){
            //     // this.setPokerScaleDeck()
            // }else if(worldY === this.handCardOriY){
            //     if(this.node.scale != 1)
            //         this.setPokerScale(1)
            // }
        }
    }

    setDisPokerAttr(fromDisPokerStack,isThisDisPokerDragable){
        this.fromDisPokerStack = fromDisPokerStack;
        this._disPokerDragable = isThisDisPokerDragable;
    }

    _initPoker (){
        this.iconMix.active = false

        this.node.on(cc.Node.EventType.TOUCH_START,this._onDragItemMgrTouchBegin.bind(this),this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE,this._onDragItemMgrTouchMoving.bind(this),this);
        this.node.on(cc.Node.EventType.TOUCH_END,this._onDragItemMgrTouchEnd.bind(this),this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,this._onDragItemMgrTouchCancel.bind(this),this);
    }

    /**
     *
     * @param pokerObj
     * @param handCardOriY  Y of world
     * @param deckOriY  y of world
     */
    initPoker (pokerObj,handCardOriY,deckOriY,mixNum) {
        // cc.log("初始化牌：",handCardOriY,deckOriY)
        this.node.poker = pokerObj
        this.pokerObj = pokerObj
        let path = cc.gg.resUtil.getPokerFramePath(pokerObj.poker)
        cc.gg.resUtil.setPokerFrame(this.node.getComponent(cc.Sprite),"pokers/"+path)

        this.iconMix.active = cc.gg.dataMgr.isJoker(pokerObj.poker) ? false : cc.gg.dataMgr.isMixCard(pokerObj.poker,mixNum)
        this.handCardOriY = handCardOriY
        this.deckOriY = deckOriY
        this.areaDis = Math.abs(handCardOriY-deckOriY)
    }

    _onDragItemMgrTouchBegin (event) {
        if(!cc.gg.tpgMgr.canDragHandPokers || this.inDiscardAnim){
            return
        }

        if(this.fromDisPokerStack && !cc.gg.tpgMgr.canGrab){
            return
        }

        if(this.fromDisPokerStack && !this._disPokerDragable)
            return

        this.startDraging()

        let touchPos = event.getLocation()

        this.setFromData(this.node.position,this.node.zIndex,this.node.scaleX)

        this.startPos = touchPos

        cc.gg.emitEvent("player_drag_poker_start",this.node,touchPos)
    }

    _onDragItemMgrTouchMoving (event) {
        if(!cc.gg.tpgMgr.canDragHandPokers || this.inDiscardAnim){
            return
        }

        if(this.fromDisPokerStack && !cc.gg.tpgMgr.canGrab){
            return
        }

        if(this.fromDisPokerStack && !this._disPokerDragable)
            return

        if(!this.dragLasting)
            return

        let delta = event.getDelta()

        this.node.x += delta.x
        this.node.y += delta.y
        this.node.zIndex = 99

        cc.gg.emitEvent("player_drag_poker_move",this.node,event)
    }

    _onDragItemMgrTouchEnd (event) {
        if(!cc.gg.tpgMgr.canDragHandPokers || this.inDiscardAnim){
            return
        }

        if(this.fromDisPokerStack && !cc.gg.tpgMgr.canGrab){
            return
        }

        if(this.fromDisPokerStack && !this._disPokerDragable)
            return

        if(!this.dragLasting)
            return

        this.stopDraging()

        let touchPos = event.getLocation()
        var moveDis = this.startPos.sub(touchPos).mag()

        if(moveDis < 10){
            this.node.zIndex = this.dragOriZIndex
            this.node.position = this.dragFromPos

            cc.gg.emitEvent("player_click_poker",this.node)
        }else
            cc.gg.emitEvent("player_drag_poker_end",this.node,touchPos)
    }

    _onDragItemMgrTouchCancel (event) {
        if(!cc.gg.tpgMgr.canDragHandPokers || this.inDiscardAnim){
            return
        }

        if(this.fromDisPokerStack && !cc.gg.tpgMgr.canGrab){
            return
        }

        if(this.fromDisPokerStack && !this._disPokerDragable)
            return

        if(!this.dragLasting)
            return

        this.stopDraging()
        let touchPos = event.getLocation()
        cc.gg.emitEvent("player_drag_poker_end",this.node,touchPos)
    }

    startDraging () {
        this.inHandStatic = false
        this.dragLasting = true
    }

    stopDraging () {
        this.draggingNode = null
        this.dragLasting = false
    }

    setPokerScale(scale){
        if(this.node.scale !== scale){
            // cc.log("扑克牌的缩放变成了：",scale)
            this.node.scale = scale
        }
    }

    setFromData (pos, zIndex, scale) {
        this._hasFromData = true
        this.dragFromPos = pos
        this.dragFromScale = scale
        this.dragOriZIndex = zIndex
    }

    showSelected (t) {
        if(this._isSelected){
            cc.warn("Poker is already selected,logic error 1!")
            return
        }

        this._isSelected = true
        this.node.y += 20
    }

    cancelSelected () {
        if(!this._isSelected){
            return
        }

        this._isSelected = false
        this.node.y -= 20
    }

    returnToOriginPos () {
        if(this._hasFromData){
            cc.log("return to origin pos:",this.dragFromPos)
            this.node.position = this.dragFromPos
            this.node.zIndex = this.dragOriZIndex
            this.node.setScale(this.dragFromScale)
        }
        // this.node.scaleX = this.dragFromScale
        // this.node.scaleY = this.dragFromScale
        this._hasFromData = false
        cc.log("恢复到原来的scale：",this.dragFromScale)
        this.dragFromPos = null
        this.dragOriZIndex = null
        this.dragFromScale = null
    }
}
